/*
 * @description :获取时间
 * @params:
 * @author : peng huaneng
 * @date : 2018/1/23 9:53
 */
function getDate( location ) {
    laydate.render( {
        elem : '#' + location ,    //指定元素
        type : 'date' ,
        trigger : 'click' ,       //单击确认 只适合 date
        lang : 'en' ,             //国际版
        showBottom : true ,     //不显示底部
        min : -31 ,
        max : 0
    } );
}

var vm = new Vue( {
    el : "#myOrder" ,
    data : {
        loading: true,
        loading1: false,
        user_id : '' ,
        user_name : '' ,
        user_image : '' ,
        city : '' ,
        web_token : '' ,
        searchLists : [] ,  //订单列表
        orderLists : [] ,  //类型订单列表
        detailsList : [] ,  //查看物流列表
        evaluationList : [] ,  //评价商品列表
        product : [] ,  //评价商品数据信息
        pageNo : 1 ,
        paginate:'',
        starTime:'',
        endTime:'',
        changes : {
            type : ''
        } ,
        comment : '' ,
        complaint_tel : '' , // 投诉联系电话
        order_id : '' ,
        supplier_id : '' ,
        goodsNum : '' ,
        image : '' ,
        firstImage : '' ,
        firstName : '' ,
        firstNameEnglish : '' ,
        tel : '' , // 商家电话
        complain_content : '' , // 投诉内容
        complain_img : [] , // 投诉上传图
        complain_tel : '' , // 投诉联系电话
        url : oss_url,
        nodata:''
    } ,
    methods : {
        // 跳转编辑个人信息
        goMyinfo : function () {
            location.href = "/userweb/index/editMyInfo.html";
        } ,
        // 不同的订单类型
        change : function ( type ) {
            this.loading    = true;
            var url = '/userweb/My/myOrder';
            if ( type == 0 ) {
                this.changes.type = 0;          //全部
                this.pageNo = 1;
                $( "#dqrOrder" ).hide();
                $( "#dpjOrder" ).hide();
            } else if ( type == 1 ) {
                this.changes.type = 1;          //待处理
                this.pageNo = 1;
                $( "#dqrOrder" ).hide();
                $( "#dpjOrder" ).hide();
            } else if ( type == 2 ) {
                this.changes.type = 2;          //待发货
                this.pageNo = 1;
                $( "#dqrOrder" ).hide();
                $( "#dpjOrder" ).hide();
            } else if ( type == 3 ) {
                this.changes.type = 3;          //待确认
                this.pageNo = 1;
                $( "#dqrOrder" ).show();
                $( "#dpjOrder" ).hide();
            } else if ( type == 4 ) {           //待评价
                this.changes.type = 4;
                this.pageNo = 1;
                $( "#dqrOrder" ).hide();
                $( "#dpjOrder" ).show();
            } else if ( type == 6 ) {           // 取消
                this.changes.type = 6;
                this.pageNo = 1;
                $( "#dqrOrder" ).hide();
                $( "#dpjOrder" ).hide();
            }
            $( "#dqrOrder_check_logistics" ).hide();
            $( "#dpjOrder_complaints" ).hide();
            $( "#dpjOrder_evaluation" ).hide();
            var data = {
                user_id :   this.user_id,
                page :      this.pageNo ,
                type :      this.changes.type,
                starTime :  this.starTime ,
                endTime :   this.endTime
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                this.orderLists = '';
                if (res.data.status == 200) {
                    this.orderLists = res.data.order.data;
                    this.paginate   = res.data.paginate;
                    for (var i=0;i<this.orderLists.length;i++){
                        this.orderLists[i]['image'] = this.url+this.orderLists[i]['image'];
                        for (var k=0;k<this.orderLists[i]['product'].length;k++){
                            this.orderLists[i]['product'][k]['image'] = this.url+this.orderLists[i]['product'][k]['image'];
                        }
                    }
                    console.log(this.orderLists);
                }
                if (this.orderLists.length == 0){
                    $('#noData').html(this.nodata);
                    this.paginate   = '';
                    this.loading = false;
                }else {
                    var _this = this;
                    setTimeout(function () {
                        _this.loading = false;
                    }, 300);
                    $('#noData').html("");
                }
            } , function ( res ) {

            } );
        } ,
        // 查看交易详情
        details : function ( id ) {
            this.loading = true;
            var url = '/userweb/my/orderTransport';
            var data = {
                order_id : id
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.data.status == 200 ) {
                    this.detailsList = res.data.order.list;
                    this.detailsList['image'] = this.url+this.detailsList['image'];
                } else {
                    layer.msg( res.data.msg ,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                this.loading = false;
            } , function ( res ) {

            } );
        } ,
        // 提醒发货
        remindDelivery : function ( order_id , index ) {
            var url = '/userweb/my/remindDelivery';
            var data = {
                order_id : order_id,
                user_id : this.user_id
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.data.status == 200 ) {
                    this.firstImage = this.orderLists[ index ].product[ 0 ].image;
                    this.firstName  = this.orderLists[ index ].product[ 0 ].product_name;
                    $( "#confirmOrder" ).show();
                    $( '#overhidden' ).css( 'overflow' , 'hidden' );
                } else {
                    this.firstImage = this.orderLists[ index ].product[ 0 ].image;
                    this.firstName = this.orderLists[ index ].product[ 0 ].product_name;
                    $( "#confirmAgain" ).show();
                    $( '#overhidden' ).css( 'overflow' , 'hidden' );
                }
            } , function ( res ) {

            } );
        } ,
        // 确认收货
        receipt : function (order_id) {
            this.loading = true;
            var url = '/userweb/My/confirmReceipt';
            var data = {
                order_id : order_id
            };
            this.$http.post(url,data,{
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.data.status == 200 ) {
                    /*this.orderLists = '';
                    this.paginate   = '';*/
                    this.change(3);
                }else {
                    layer.msg( res.data.msg ,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                this.loading = false;
            } , function ( res ) {

            } );
        } ,
        // 订单搜索
        search : function () {
            this.starTime   = $( '#startTime' ).val();
            this.endTime    = $( '#endTime' ).val();
            if (!this.endTime){
                layer.msg('请填写结束时间</br>Please fill in the end time',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (!this.starTime){
                layer.msg('请填写开始时间</br>Please fill in the start time',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (this.starTime>this.endTime){
                layer.msg('开始时间不能大于结束时间</br>The start time is not greater than the end time',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            this.change();
        } ,
        // 评价内容获取
        getEvaluate : function ( order_id , index , supplier_id ) {
            this.supplier_id = supplier_id;
            this.order_id = order_id;
            var url = '/userweb/my/myOrderEvaluated';
            var data = {
                order_id : order_id
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.data.status == 200 ) {
                    this.evaluationList = res.data.order.list;
                    this.goodsNum = res.data.order.count;
                    for (var i=0;i<this.evaluationList.length;i++){
                        this.evaluationList[i]['image'] = this.url+this.evaluationList[i]['image'];
                    }
                    setTimeout( function () {
                        for ( var i = 0 ; i < index ; i++ ) {
                            $( '#goods' + i ).raty( {
                                path : '/js/star/img' ,
                                size : 40 ,
                                starOff : 'star2.png' ,
                                starOn : 'star1.png'
                            } );
                        }
                    } , 100 );
                } else {
                    layer.msg( res.data.msg ,{
                        skin:'winning-class',
                        time:3000
                    });
                }
            } , function ( res ) {

            } );

        } ,
        // 评价订单提交
        evaluate : function () {
            this.loading = true;
            var arr = [];
            for ( var i = 0 ; i < this.goodsNum ; i++ ) {
                var obj = {
                    star : '' ,
                    comment : '',
                    product_id : ''
                };
                var goodsStar = $( "#goods" + i ).find( "input" ).val();
                if ( '' == goodsStar ) {
                    layer.msg( '请对商品评分</br>Please score the product' ,{
                        skin:'winning-class',
                        time:3000
                    });
                    this.loading = false;
                    return;
                }
                var goodsComment = $( "#comment" + i ).val();
                var goodsID = $( "#id" + i ).val();
                //console.log( goodsStar );
                obj.star = goodsStar;
                obj.comment = goodsComment;
                obj.product_id = goodsID;
                arr.push( obj );
            }
            var store_star = $( "#star1" ).find( "input" ).val();
            var description_star = $( "#star2" ).find( "input" ).val();
            var logistics_services_star = $( "#star3" ).find( "input" ).val();
            var service_attitude_star = $( "#star4" ).find( "input" ).val();
            if ( '' == store_star ||'' == description_star || '' == logistics_services_star || '' == service_attitude_star ) {
                layer.msg( '请对店铺评分</br>Please score the store' ,{
                    skin:'winning-class',
                    time:3000
                });
                this.loading = false;
                return;
            }
            var url = '/userweb/my/confirmEvaluated';
            var data = {
                user_id : localStorage.getItem( 'user_id' ) ,
                supplier_id : this.supplier_id ,
                order_id : this.order_id ,
                store_star : '' ,
                description_star : description_star ,
                product : arr ,
                comment : this.comment ,
                store_star : store_star,
                logistics_services_star : logistics_services_star ,
                service_attitude_star : service_attitude_star
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.data.status == 200 ) {
                    // layer.msg( '评价成功</br>Evaluation success' ,{
                //     skin:'winning-class',
                //         time:3000
                // });
                    setTimeout( "location.href = '/userweb/index/myOrder.html'" , 1000 );
                } else {
                    layer.msg( res.data.msg ,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);
            } , function ( res ) {

            } );
        } ,
        complaints : function ( order_id ) {
            this.order_id = order_id;
            $( "#dpjOrder" ).hide();
            $( "#dpjOrder_complaints" ).show();
        } ,
        evaluation : function ( order_id , index , supplier_id ) {
            this.getEvaluate( order_id , index , supplier_id );
            this.supplier_id = supplier_id;
            $( "#dpjOrder" ).hide();
            $( "#dpjOrder_evaluation" ).show();

        } ,
        // 取消订单
        showCancel : function ( tel ) {
            this.tel = tel;
            $( "#cancelOrder" ).show();
            $( '#overhidden' ).css( 'overflow' , 'hidden' );
        } ,
        changeStar : function ( index ) {
            var score = $( "#goods" + index + " input" ).val();
            if ( 3 <= score ) {
                this.setGood( index );
            } else {
                this.setBad( index );
            }
        } ,
        setGood : function ( index ) {
            var good = $( "#good" + index );
            var bad = $( "#bad" + index );
            good.css( 'color' , '#FF6A0E' );
            good.css( 'border' , '1px solid #FF6A0E' );
            good.find( 'img' ).attr( 'src' , '/image/userweb/personal/praise.png' );
            bad.css( 'color' , '#999999' );
            bad.css( 'border' , '1px solid #999999' );
            bad.find( 'img' ).attr( 'src' , '/image/userweb/personal/breview1.png' );
        } ,
        setBad : function ( index ) {
            var good = $( "#good" + index );
            var bad = $( "#bad" + index );
            good.css( 'color' , '#999999' );
            good.find( 'img' ).attr( 'src' , '/image/userweb/personal/praise1.png' );
            good.css( 'border' , '1px solid #999999' );
            bad.css( 'color' , '#FF6A0E' );
            bad.css( 'border' , '1px solid #FF6A0E' );
            bad.find( 'img' ).attr( 'src' , '/image/userweb/personal/breview.png' );
        } ,
        // 上传投诉图片
        uploadImg : function ( e ) {
            this.loading1 = true;
            var data = new FormData() , _this = this , html = '' , url = '/apishop/Product/uploadImg' , name , ext;
            $.each( e.target.files , function ( i , v ) {
                data.append( e.target.id + '[]' , v );
            } );
            data.append( 'dir' , 'complain' );
            data.append( 'name' , e.target.id );
            axios.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                if ( res.status == 200 ) {
                    //console.log(res)
                    $.each( res.data[ e.target.id ] , function ( i , v ) {
                        _this[ e.target.id ].push( v );
                    } );
                    $.each( _this[ e.target.id ] , function ( i , v ) {
                        name = v.split( '/' )[ 1 ].split( '.' )[ 0 ];
                        ext = v.split( '/' )[ 1 ].split( '.' )[ 1 ];
                        html += "<div style=\"position: relative;\" class='form-group' id='" + name + "' >";
                        html += "<img src=' "+oss_url+ v + "' />";
                        html += "<div  class='img_remove_a' onclick='removeNodes(\"" + name + "\",\"" + ext + "\",\"" + e.target.id + "\")'></div>";
                        html += "</div>";
                    } );
                    $( '.' + e.target.id ).html( html );
                 }
                setTimeout(function () {
                    _this.loading1 = false;
                 }, 300);
            } , function ( res ) {

            } );

        } ,
        // 订单投诉
        complain : function () {
            this.loading = true;
            if (this.complain_content.length == 0){
                layer.msg('请填写投诉内容</br>Please fill in the complaint',{
                    skin:'winning-class',
                    time:3000
                });
                this.loading = false;
                return false;
            }
            var data , url = '/userweb/My/complains';
            data = {
                user_id : this.user_id,
                order_id : this.order_id ,
                content : this.complain_content ,
                image : this.complain_img ,
                tel : this.complain_tel
            };
            axios.post( url , data , {
                emulateJSON : true
            } ).then( function ( res ) {
                var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                    location.reload();
                }, 300);
            } ), function ( res ) {

            }
        }
    } ,

    mounted : function () {
        this.nodata += '<div class="my_no-data">';
        this.nodata += '<div class="no_data_tip">';
        this.nodata += '<img src="/image/userweb/slicesImg/no-data.png" alt="">';
        this.nodata += '<div class="no-data-text">';
        this.nodata += '<div class="title_zh">暂无数据</div>';
        this.nodata += '<div class="title_en">No data</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        this.user_id    = localStorage.getItem( 'user_id' );
        this.user_name  = localStorage.getItem( 'user_name' );
        this.user_image = localStorage.getItem( 'user_image' );
        this.city = localStorage.getItem( 'city' );
        this.web_token = localStorage.getItem( 'web_token' );
        if ( !this.user_id ) {
            location.href = "/userweb/index/login.html";
        }
        this.change(2);
    }
} );


//删除图片
function removeNodes( name , ext , id ) {
    vm.loading1 = true;
    var path = 'product/' + name + '.' + ext , num = $.inArray( path , vm[ id ] );
    var data = { path : path } , url = '/apishop/Product/delImg';
    $.ajax( {
        url : url ,
        dataType : 'json' ,
        type : 'POST' ,
        data : data ,
        success : function ( res ) {
            if ( res.status == 200 ) {
                vm[ id ].splice( num , 1 );
                $( "#" + name ).remove();
                $( "#" + id ).val( '' );
            }
            setTimeout(function () {
                vm.loading1 = false;
            }, 300);
        } ,
        error : function () {
            layer.msg( '网络错误！' ,{
                skin:'winning-class',
                time:3000
            });
        }
    } );
}

function AjaxPage(page) {
    vm.pageNo = page;
    vm.change();
}
function jump(){
    var page = $("#page_no").val();
    vm.pageNo = page;
    vm.change();
}